<?php
	
	/**
	 * 添加商品前置操作
	 * @param $model
	 * 添加套餐商品关联记录
	 */
	function product_add_before($model){
		
		//套餐商品，添加套餐商品关联记录
		if($model->ispackage==1){
			
			if($model->isintegral){
				$model->isintegral=1;
			}else{
				$model->isintegral=2;
			}
		}else{
			
			$gcnum   = $_POST['gcnum'];
			$gmprice = $_POST['gmprice'];
			$gsprice = $_POST['gsprice'];
			$garray  = array();
			
			if(is_array($gcnum)){
				foreach($gcnum as $k=>$v){
					if($v&&$gmprice[$k]&&$gsprice[$k]){
						
						$garray[$v['cnum']]['cnum']   = $v;
						$garray[$v['cnum']]['mprice'] = $gmprice[$k];
						$garray[$v['cnum']]['sprice'] = $gsprice[$k];
					}
				}
			}
			
			if($model->isspecial){
				$model->isspecial =1; 
			}else{
				$model->isspecial =2;
			}
			
			if($model->isdiscount){
				$model->isdiscount=1; 
			}else{
				$model->isdiscount=2;
			}
			
			if($model->isintegral){
				$model->isintegral=1; 
			}else{
				$model->isintegral=2;
			}
			
			$model->attr = json_encode($garray);
		}
		
		if($model->title){
			$str = mb_substr($model->title,0,1,'utf-8');
			
			$model->initials = $model->is_chinese($str);
		}
		
		if($model->discount){
			$model->discount = ($model->discount)/100;
		}
		
		return $model;
	}
	
	
	function product_add_after($table, $id, $action){
		
		$where['packid'] = $id;
		M('ProductPackage')->where($where)->delete();
		
		$ids     = $_POST['ids'];     //所选商品id
		$typeids = $_POST['typeids']; //商品计费类型
		$cnums   = $_POST['cnums'];   //商品计费周期
		$units   = $_POST['units'];   //商品计费单位
		$amounts = $_POST['amounts']; //商品数量
		$model   = M("$table");
		$rows    = $model->find($id);
		
		//添加套餐记录
		foreach($ids as $k=>$v){
		
			$map['company'] = $rows['company'];
			$map['price']   = $_POST['sprice'];
			$map['sort']    = $rows['sort'];
			$map['packid']  = $id;
			$map['pid']     = $v;
			$map['addtime'] = time();
			$map['solevar'] = create_id();
			$map['typeid']  = $typeids[$k];
			$map['cnum']    = $cnums[$k];
			$map['unit']    = $units[$k];
			$map['amount']  = $amounts[$k];
		
			M('ProductPackage')->add($map);
		}
	}
	
?>
